<template>
  <div class="xtx-city">
    <div class="select" @click="toggleAtive">
      <span class="placeholder" v-if="!provinceName">请选择配送地址</span>
      <span class="value">
          {{provinceName}}
          {{cityName}}
          {{countyName}}
          </span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span class="ellipsis" v-for="i in filterData" :key="i" @click="changeData(i)">{{i.name}}</span>
    </div>
  </div>
</template>

<script>
import { onMounted, ref, reactive, computed, toRefs, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  setup () {
    // 控制展开收起,默认收起
    const dataList = ref([])
    function loadCityList () {
      axios({
        url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json',
        method: 'GET'
      }).then((res) => {
        dataList.value = res.data
      })
    }
    onMounted(() => {
      loadCityList()
    })
    const active = ref(false)
    function toggleAtive () {
      active.value = !active.value
    }
    // 城市筛选交互
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '' // 地区名称
    })

    // 重铸数据
    watch(() => { return active.value }, (newVal) => {
      if (newVal === true) {
        changeResult.provinceCode = ''
        changeResult.provinceName = ''
        changeResult.cityCode = ''
        changeResult.cityName = ''
        changeResult.countyCode = ''
        changeResult.countyName = ''
        console.log('看看数据', changeResult)
      }
    })

    // 定义计算属性
    // 核心逻辑:
    // 1. 默认情况下provinceCode和cityCode都没有,所以直接返回list,渲染所有省份
    // 2. 当provinceCode有数据之后,list变成了当前省份对应的所有城市数据
    // 3. 当cityCode有数据之后,list变成了当前城市下所有的地区
    function changeData (curItem) {
      if (curItem.level === 0) {
        // 给省赋值
        changeResult.provinceCode = curItem.code
        changeResult.provinceName = curItem.name
      }
      if (curItem.level === 1) {
        // 给市赋值
        changeResult.cityCode = curItem.code
        changeResult.cityName = curItem.name
      }
      if (curItem.level === 2) {
        // 给区域赋值
        changeResult.countyCode = curItem.code
        changeResult.countyName = curItem.name
      }
    }
    const filterData = computed(() => {
      let _dataList = dataList.value
      //   如果省code已经存在，就是用当前code，去整个省列表中寻找符合条件的省数据下的areaList交给_dataLo=ist
      if (changeResult.provinceCode) {
        const curObj = dataList.value.find((item) => item.code === changeResult.provinceCode)
        _dataList = curObj.areaList
      }
      //   如果市code已经存在，就是用当前code，去整个市列表中寻找符合条件的省数据下的areaList交给_dataLo=ist
      if (changeResult.cityCode) {
        const curObj = _dataList.find((item) => item.code === changeResult.cityCode)
        _dataList = curObj.areaList
      }
      //   如果区code已经存在，就是用当前code，去整个区列表中寻找符合条件的省数据下的areaList交给_dataLo=ist
      if (changeResult.countyCode) {
        // 全部交互完毕关闭弹窗

        // eslint-disable-next-line vue/no-side-effects-in-computed-properties
        active.value = false
      }
      return _dataList
    })
    return { toggleAtive, active, ...toRefs(changeResult), changeData, filterData }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
